{% extends 'base.html' %}

{% block title %}图床管理{% endblock %}

{% block content %}
<div class="card mb-4">
    <div class="card-header">
        <h3 class="card-title">文件上传</h3>
    </div>
    <div class="card-body">
        <form method="post" enctype="multipart/form-data">
            {{ form.csrf_token }}
            <div class="mb-3">
                {{ form.file.label(class="form-label") }}
                {{ form.file(class="form-control") }}
                <div class="form-text">支持上传任何类型的文件</div>
                {% if form.file.errors %}
                    <div class="invalid-feedback d-block">
                        {% for error in form.file.errors %}
                            {{ error }}
                        {% endfor %}
                    </div>
                {% endif %}
            </div>
            <button type="submit" class="btn btn-primary">{{ form.submit.label }}</button>
        </form>
    </div>
</div>

{% if uploads %}
    {% set images = [] %}
    {% set others = [] %}
    {% for file in uploads %}
        {% if file.is_image %}
            {% set _ = images.append(file) %}
        {% else %}
            {% set _ = others.append(file) %}
        {% endif %}
    {% endfor %}

    {% if images %}
    <div class="row row-cols-1 row-cols-md-3 row-cols-lg-4 g-4 mb-4">
        {% for file in images %}
        <div class="col">
            <div class="card h-100 shadow-sm position-relative group-hover">
                <img src="{{ url_for('static', filename='uploads/' + file.name) }}" 
                     class="card-img-top" 
                     alt="{{ file.original_name }}"
                     style="height: 200px; object-fit: cover; cursor: pointer;"
                     onclick="window.open(this.src, '_blank')">
                <div class="card-body p-2">
                    <p class="card-text text-truncate small mb-0" title="{{ file.original_name }}">
                        {{ file.original_name }}
                    </p>
                </div>
                <div class="card-footer bg-transparent border-top-0 p-2 d-flex justify-content-between align-items-center">
                    <div class="btn-group btn-group-sm">
                        <a href="{{ url_for('admin.download_upload', id=file.id) }}" class="btn btn-outline-secondary" title="下载">
                            <i class="bi bi-download"></i>
                        </a>
                        <form method="post" action="{{ url_for('admin.delete_upload', id=file.id) }}" class="d-inline">
                            <button type="submit" class="btn btn-outline-danger" onclick="return confirm('确定要删除吗？')" title="删除">
                                <i class="bi bi-trash"></i>
                            </button>
                        </form>
                    </div>
                    <button class="btn btn-sm btn-primary" onclick="copyLink('{{ url_for('static', filename='uploads/' + file.name) }}', this)" title="复制链接">
                        <i class="bi bi-link-45deg"></i> 复制
                    </button>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
    {% endif %}

    {% if others %}
    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">其他文件</h5>
        </div>
        <div class="list-group list-group-flush">
            {% for file in others %}
            <div class="list-group-item d-flex justify-content-between align-items-center">
                <div class="d-flex align-items-center">
                    <div class="d-flex justify-content-center align-items-center bg-light border rounded me-3" 
                         style="width: 40px; height: 40px;">
                        <i class="bi bi-file-earmark fs-4 text-secondary"></i>
                    </div>
                    <div>
                        <div class="fw-bold text-truncate" style="max-width: 300px;">{{ file.original_name }}</div>
                        <small class="text-muted">{{ file.name }}</small>
                    </div>
                </div>
                <div class="btn-group btn-group-sm">
                    <button class="btn btn-outline-primary" onclick="copyLink('{{ url_for('static', filename='uploads/' + file.name) }}', this)">
                        <i class="bi bi-link-45deg"></i>
                    </button>
                    <a href="{{ url_for('admin.download_upload', id=file.id) }}" class="btn btn-outline-secondary">
                        <i class="bi bi-download"></i>
                    </a>
                    <form method="post" action="{{ url_for('admin.delete_upload', id=file.id) }}" class="d-inline">
                        <button type="submit" class="btn btn-outline-danger" onclick="return confirm('确定要删除吗？')">
                            <i class="bi bi-trash"></i>
                        </button>
                    </form>
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
    {% endif %}

    <script>
    function copyLink(url, btn) {
        var aux = document.createElement("input"); 
        aux.setAttribute("value", url); 
        document.body.appendChild(aux); 
        aux.select();
        
        try {
            document.execCommand("copy");
            // UI Feedback
            const originalHtml = btn.innerHTML;
            btn.innerHTML = '<i class="bi bi-check"></i> 已复制';
            
            // Handle both outline and solid button styles
            if (btn.classList.contains('btn-primary')) {
                btn.classList.replace('btn-primary', 'btn-success');
                setTimeout(() => {
                    btn.innerHTML = originalHtml;
                    btn.classList.replace('btn-success', 'btn-primary');
                }, 2000);
            } else if (btn.classList.contains('btn-outline-primary')) {
                btn.classList.replace('btn-outline-primary', 'btn-outline-success');
                setTimeout(() => {
                    btn.innerHTML = originalHtml;
                    btn.classList.replace('btn-outline-success', 'btn-outline-primary');
                }, 2000);
            }
            
        } catch (err) {
            alert("复制失败，请手动复制");
        }
        
        document.body.removeChild(aux);
    }
    </script>
{% endif %}
{% endblock %}